package com.server.core.init;

import com.server.core.handle.HttpServerHandler;
import com.server.core.handle.ws.WebSocketServerHandler;
import com.server.core.util.HttpsUtil;
import com.server.core.util.PropertiesUtil;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

import javax.net.ssl.SSLEngine;

public class DefautInitializet extends ChannelInitializer<SocketChannel> {
    PropertiesUtil prop = PropertiesUtil.getInstance();
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
         ChannelPipeline pipeline = socketChannel.pipeline();

         ///是否开启https
         if(prop.getProperty("SSL_ON")=="true") {
             SSLEngine sslEngine = HttpsUtil.getSslContext().createSSLEngine();
             sslEngine.setUseClientMode(false);
             pipeline.addLast(new SslHandler(sslEngine));
         }

         pipeline.addLast(new HttpServerCodec());
                //把多个消息转换为一个单一的FullHttpRequest或是FullHttpResponse
         pipeline.addLast(new HttpObjectAggregator(65536))
                //压缩Http消息
                .addLast(new ChunkedWriteHandler())
                //大文件支持
                ;
         pipeline.addLast(new HttpServerHandler());
         /// web socket handle
         pipeline.addLast("handler",
                new WebSocketServerHandler());
    }
}


